package com.smm.web.vmc.service.impl;

import com.smm.web.vmc.dao.BaseDao;
import com.smm.web.vmc.domain.Tag;
import com.smm.web.vmc.service.AlamService;
import com.smm.web.vmc.service.TagService;
import com.smm.web.vmc.service.WarehouseService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhenghao on 2016/9/7.
 *
 */
@Service
public class TagServiceImpl implements TagService {


    @Resource
    private BaseDao<Tag> tagBaseDao;

    @Resource
    private WarehouseService warehouseService;

    @Resource
    private AlamService alamService;


    @Override
    public List<Tag> getAllByWarehouse(int whId) {

        DetachedCriteria where = DetachedCriteria.forClass(Tag.class);
        where.add(Restrictions.eq("warehouse.id", whId));
        where.addOrder(Order.desc("no"));

        return tagBaseDao.query(where);
    }

    @Override
    public void delete(String no ) throws Exception {

        //获取标签对象
        Tag tag = tagBaseDao.getOne(Tag.class,no);

        if(tag==null) return;

//        //检查是否关摄像头， 如果是，不能删除
//        if(tag.getCamera()!=null) throw new Exception("该RFID与摄像头"+tag.getCamera().getName()+"绑定，不能删除");
//
//        //检查是否关联仓单，如果是，不能删除
//        if(tag.getReceipt()!=null) throw new Exception("该RFID与仓单"+tag.getReceipt().getNo()+"绑定，不能删除");
//
//        //清空标签关联仓库对象
//        tag.setWarehouse(null);
//
////        tagBaseDao.delete(tagBaseDao.getOne(Tag.class,no ));
//
//        tagBaseDao.update(tag);
//
//        tagBaseDao.delete(tag);

        //清空该标签关联的所有报警记录
        alamService.deleteAllByTag(no);

        //删除标签
        tagBaseDao.delete(tag);

    }

    @Override
    public void add(String no,int whId) throws Exception{

        Tag tag = new Tag();

        tag.setNo(no);
        tag.setWarehouse(warehouseService.getOneById(whId));

        tagBaseDao.add(tag);

    }

    @Override
    public List<Tag> getAllByCamer(int camerId) {

        DetachedCriteria where = DetachedCriteria.forClass(Tag.class);
        where.add(Restrictions.eq("camera.id", camerId));

        return tagBaseDao.query(where);
    }

    @Override
    public void update(Tag tag) throws Exception {

        tagBaseDao.update(tag);
    }

    @Override
    public List<Tag> getAllByReceipt(String repNo) {

        DetachedCriteria where = DetachedCriteria.forClass(Tag.class);
        where.add(Restrictions.eq("receipt.no", repNo));

        return tagBaseDao.query(where);
    }

    @Override
    public Tag getOneById(String tagNo) {

        return tagBaseDao.getOne(Tag.class,tagNo);
    }

    @Override
    public List<Tag> getAllByWarehouseAndNoRep(int whId) {

        DetachedCriteria where = DetachedCriteria.forClass(Tag.class);
        where.add(Restrictions.eq("warehouse.id", whId));
        where.add(Restrictions.isNull("receipt"));
        where.addOrder(Order.desc("no"));

        return tagBaseDao.query(where);
    }

    @Override
    public List<Tag> getAllByWarehouseAndNotThisRep(int whId, String repNo) {

        DetachedCriteria where = DetachedCriteria.forClass(Tag.class);
        where.add(Restrictions.eq("warehouse.id", whId));
        where.add(Restrictions.or(
                Restrictions.isNull("receipt"),
                Restrictions.eq("receipt.no",repNo)
        ) );
        where.addOrder(Order.desc("no"));

        return tagBaseDao.query(where);
    }

    @Override
    public void deleteAllByReceipt(String repNo) throws Exception {

        List<Tag> tagList = this.getAllByReceipt(repNo);

        if(tagList!=null && tagList.size()>0){

            for(Tag tag:tagList){

                tag.setReceipt(null);

                this.tagBaseDao.update(tag);
            }
        }
    }
}
